import store from '@/store'
import tools from '@/utils/tools'
import CRC from '@/utils/crc16'
import { aesEncrypt } from '@/secret/secret'
import { bodyDataController, setBodyDataControllerData, clearBodyDataController } from './socketSendTools'

// 处理websocket发送
export const sendSock = (agentData, callback) => {
  const dataDealer = tools.dataDealer
  if (callback) {
    store.commit('socket/set_globalCallback', callback)
  }
  if (store.state.socket.webSocket.readyState === store.state.socket.webSocket.OPEN) {
    // 若是开启状态
    const typeList = agentData.type.split('.')
    // 协议版本string => uint8Array byteLength: 2
    agentData.version = dataDealer.strToUint8(process.env.VUE_APP_SOCKET_VERSION, Uint8Array)
    // 请求编号动态加1
    store.commit('socket/set_req_id')
    // 请求编号uint32Array => uint8Array byteLength: 4
    agentData.req_id = dataDealer.numToUint8(store.state.socket.req_id, Uint32Array)
    // 设置reqList 保证响应与请求匹配
    store.commit('socket/set_reqList_addKeyValue', { key: store.state.socket.req_id, type: agentData.type })
    // 消息主类型 uint8 => uint8Array byteLength: 1
    agentData.msg_type = dataDealer.numToUint8(+typeList[0], Uint8Array)
    // 消息子类型1 uint8 => uint8Array byteLength: 1
    agentData.sub_type1 = dataDealer.numToUint8(+typeList[1], Uint8Array)
    // 消息子类型2 uint8 => uint8Array byteLength: 1
    agentData.sub_type2 = dataDealer.numToUint8(+typeList[2], Uint8Array)
    if (agentData.type !== '5.1.3') {
      //
    }
    if (agentData.type === '4.2.3') {
      //
    }
    // body 内容转uint8
    switch (agentData.type) {
      case '5.1.1': {
        break
      }
      case '5.1.3': {
        break
      }
      default: {
        setBodyDataControllerData(agentData.body)
        agentData.body = bodyDataController.filter(item => {
          return item.data
        })
        for (let i = 0; i < agentData.body.length; i++) {
          agentData.body[i] = agentData.body[i].data
        }
        agentData.body = dataDealer.concatenate(Uint8Array, ...agentData.body)
        delete agentData.type
        agentData.body = aesEncrypt(agentData.body, store.state.socket.DHSecretKey)
        break
      }
    }
    // 获取body长度 num => uint32 => uint8
    agentData.bodyLength = dataDealer.numToUint8(agentData.body.length, Uint32Array)
    // 除去CRC校验项之外的其他数据提到一个数组中
    const arrays = [agentData.version, agentData.req_id, agentData.msg_type, agentData.sub_type1, agentData.sub_type2, agentData.bodyLength, agentData.body]
    // 前七项数据合并
    const sevenBuffer = dataDealer.concatenate(Uint8Array, ...arrays)
    agentData.crc = CRC.ToCRC16(sevenBuffer)
    // crc num => uint8Array byteLength: 2
    agentData.crc = dataDealer.numToUint8(agentData.crc, Uint16Array)
    // 所有数据合并
    agentData = dataDealer.concatenate(Uint8Array, sevenBuffer, agentData.crc)
    // 清空vuex-bodyDataController
    clearBodyDataController()
    store.state.socket.webSocket.send(agentData)
  } else if (store.state.socket.webSocket.readyState === store.state.socket.webSocket.CONNECTING) {
    // 若是正在开启状态
    const to = setTimeout(() => {
      sendSock(agentData, callback)
      clearTimeout(to)
    }, 1000)
  } else {
    // 若未开启 则等待一秒后调用
    const to = setTimeout(() => {
      sendSock(agentData, callback)
      clearTimeout(to)
    }, 1000)
  }
}
